package com.zone.vehicle.superApps.view.home.app.adapter;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.zone.sdk.module.appstore.base.AppStoreResult;
import com.zone.sdk.module.appstore.entity.AppEntity;
import com.zone.vehicle.superApps.R;
import com.zone.vehicle.superApps.view.home.app.main.AppHorizontalFragment.AppItemOnClickListener;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

public class HomeAppAdapterAdapter extends RecyclerView.Adapter<HomeAppAdapterAdapter.ViewHolder> {

    private static final String TAG = HomeAppAdapterAdapter.class.getSimpleName();

    public void setData(AppStoreResult<AppEntity> data) {
        this.data = data;
    }

    private AppStoreResult<AppEntity> data;

    private AppItemOnClickListener listener;

    public HomeAppAdapterAdapter(AppStoreResult<AppEntity> data, AppItemOnClickListener listener) {
        this.data = data;
        this.listener = listener;
    }

    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.fragment_home_app_item, parent, false);
        v.findViewById(R.id.item1).setOnClickListener(listener);
        v.findViewById(R.id.item2).setOnClickListener(listener);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NotNull ViewHolder holder, int position) {
        if (ObjectUtils.isEmpty(data)){
            return;
        }
        AppEntity app1 = data.getRecords().get(position*2);
        //设置图片大小
        RoundedCorners roundedCorners = new RoundedCorners(50);
        //扩展功能
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners)
                .placeholder(new ColorDrawable(Color.GRAY))//设置占位图
//                    .error(R.mipmap.logo_app)//设置异常图片
                .override(300, 300);//降低内存消耗
        if (ObjectUtils.isNotEmpty(app1)){
//            Glide.with(holder.itemView.getContext()).asBitmap().load("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Ffile03.16sucai.com%2F2015%2F06%2F2015tfkbbmq55ac.jpg").apply(options).into(holder.pic1);
            Glide.with(holder.itemView.getContext()).asBitmap().load(app1.getPreviews()).apply(options).into(holder.pic1);
//            Glide.with(holder.itemView.getContext())
//                    .asBitmap()
//                    .load(R.drawable.app_pic_demo)
//                    .apply(options)
//                    .into(holder.pic1);
            holder.itemView.findViewById(R.id.item1).setTag(app1);
//            Glide.with(holder.itemView.getContext()).load(app1.getIcon()).into(holder.icon1);
//            holder.pic1.setImageResource(R.drawable.app_pic_demo);
            holder.name1.setText(app1.getName());
//            holder.star1.setImageResource(getStarId(app1.getStar()));
            holder.score1.setText(app1.getScore());
            if (StringUtils.isNotEmpty(app1.getLabels())){
                String[] tags = app1.getLabels().split(",");
                if (tags.length >= 1) {
                    holder.tag11.setText(tags[0]);
                    holder.tag11.setVisibility(View.VISIBLE);
                }
                if (tags.length >= 2) {
                    holder.tag12.setText(tags[1]);
                    holder.tag12.setVisibility(View.VISIBLE);
                }
                if (tags.length >= 3) {
                    holder.tag13.setText(tags[2]);
                    holder.tag13.setVisibility(View.VISIBLE);
                }
            }
            holder.description1.setText(app1.getOneWord());

        }
        AppEntity app2 = data.getRecords().get(position*2+1);
        if (ObjectUtils.isNotEmpty(app2)){
            holder.itemView.findViewById(R.id.item2).setTag(app2);
            Glide.with(holder.itemView.getContext()).asBitmap().load(app2.getPreviews()).apply(options).into(holder.pic2);
//            Glide.with(holder.itemView.getContext())
//                    .asBitmap()
//                    .load(R.drawable.app_pic_demo)
//                    .apply(options)
//                    .into(holder.pic2);
//            Glide.with(holder.itemView.getContext()).load(app1.getIcon()).into(holder.icon1);
//            holder.pic2.setImageResource(R.drawable.app_pic_demo);
            holder.name2.setText(app2.getName());
//            holder.star2.setImageResource(getStarId(app2.getStar()));
            holder.score2.setText(app2.getScore());
            if (StringUtils.isNotEmpty(app2.getLabels())){
                String[] tags = app2.getLabels().split(",");
                if (tags.length >= 1) {
                    holder.tag21.setText(tags[0]);
                    holder.tag21.setVisibility(View.VISIBLE);
                }
                if (tags.length >= 2) {
                    holder.tag22.setText(tags[1]);
                    holder.tag22.setVisibility(View.VISIBLE);
                }
                if (tags.length >= 3) {
                    holder.tag23.setText(tags[2]);
                    holder.tag23.setVisibility(View.VISIBLE);
                }
            }
            holder.description2.setText(app2.getOneWord());
        }
//        Glide.with(holder.itemView.getContext())
//                .load(data.get(position*2).getImage())
//                .into(holder.image1);
    }

    @Override
    public int getItemCount() {
        if (ObjectUtils.isNotEmpty(data) && ObjectUtils.isNotEmpty(data.getRecords())) {
            return data.getRecords().size() / 2;
        }else {
            return 0;
        }
    }

    private int getStarId(int star){
        switch (star){
            case 1:
                return R.mipmap.icon_star;
            case 2:
                return R.mipmap.icon_star;
            case 3:
                return R.mipmap.icon_star;
            case 4:
                return R.mipmap.icon_star;
            case 5:
                return R.mipmap.icon_star;
            default:
                return R.mipmap.icon_star;
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private final ImageView pic1;
        private final TextView name1;
        private final ImageView star1;
        private final TextView score1;
        private final TextView tag11;
        private final TextView tag12;
        private final TextView tag13;
        private final TextView description1;

        private final ImageView pic2;
        private final TextView name2;
        private final ImageView star2;
        private final TextView score2;
        private final TextView tag21;
        private final TextView tag22;
        private final TextView tag23;
        private final TextView description2;


        public ViewHolder(View itemView) {
            super(itemView);
            pic1 = itemView.findViewById(R.id.pic1);
            name1 = itemView.findViewById(R.id.name1);
            star1 = itemView.findViewById(R.id.star1);
            score1 = itemView.findViewById(R.id.score1);
            tag11 = itemView.findViewById(R.id.tag11);
            tag12 = itemView.findViewById(R.id.tag12);
            tag13 = itemView.findViewById(R.id.tag13);
            description1 = itemView.findViewById(R.id.description1);

            pic2 = itemView.findViewById(R.id.pic2);
            name2 = itemView.findViewById(R.id.name2);
            star2 = itemView.findViewById(R.id.star2);
            score2 = itemView.findViewById(R.id.score2);
            tag21 = itemView.findViewById(R.id.tag21);
            tag22 = itemView.findViewById(R.id.tag22);
            tag23 = itemView.findViewById(R.id.tag23);
            description2 = itemView.findViewById(R.id.description2);
        }
    }



}
